import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
import {
	http
} from "../common/http.js"

export const store = new Vuex.Store({
	state: {
		userInfo: uni.getStorageSync('userInfo') || {},
		token: uni.getStorageSync('token') || '',
		city: uni.getStorageSync('city'),
		start: {},
		end: {},
		tabList: [],
		address: uni.getStorageSync('address')||[], //默认搜索的地址
	},
	mutations: {
		getTab(state, tabList) {
			state.tabList = tabList
		},
		getCity(state, city) {
			state.city = city
			uni.setStorageSync('city', state.city)
		},
		getStart(state, start) {
			state.start = start
			let flag = false;
			state.address.forEach(item => {
				if (item.address == start.address) {
				  flag=true
				}
			})
			if(!flag){
				if (state.address.length < 5) {
					state.address.unshift(start)
				} else {
					state.address.pop()
					state.address.unshift(start)
				}
			}
			uni.setStorageSync('address', state.address)
		},
		getEnd(state, end) {
			state.end = end
			let flag = false;
			state.address.forEach(item => {
				if (item.address == end.address) {
				  flag=true
				}
			})
			if(!flag){
				if (state.address.length < 5) {
					state.address.unshift(end)
				} else {
					state.address.pop()
					state.address.unshift(end)
				}
			}
			uni.setStorageSync('address', state.address)
		},
		getUserInfo(state, info) {
			//console.log(state);
			state.userInfo = info
			state.token = state.userInfo.token
			uni.setStorageSync('userInfo', state.userInfo)
			uni.setStorageSync('token', state.userInfo.token)
		},
		exitLogin(state) {
			state.userInfo = {}
			state.token = ''
			uni.setStorageSync('userInfo', state.userInfo)
			uni.setStorageSync('token', state.userInfo.token)
		},
	},
	getters: {},
	// this.$store.dispatch('updateUserInfo')
	actions: {
		updateUserInfo({
			commit
		}) {
			http('/api/ucenter/getUserInfo', "GET").then(data => {
				commit('getUserInfo', data)
			})
		}
	}
})
